package com.example.kingdee.kingdeeBridge.kingdeeImpl.impl;

import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.kingdeeBridge.dto.BillWriteInDto;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.BillImpl;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.common.DataObject;
import com.example.kingdee.sqlTool.common.QueryObject;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 生产用料清单
 */
@Service
public class PpBomImpl implements BillImpl {

    @Autowired
    private SqlTool sqlTool;

    @Autowired
    private TBxStMapper tBxStMapper;

    /**
     *查询目标单据
     * @param fid 表id
     * @return
     */
    @Override
    public Map<String, String> selectTargetBillEntry(String fid) {
        return null;
    }
    /**
     *
     * @param fid 表id
     * @param billWriteInDto 源单信息
     * @param targetInfo 目标单据行id和源单行id对应关系
     * @return
     */
    @Override
    public RequestResult updateTargetBillEntry(String fid, BillWriteInDto billWriteInDto, Map<String, String> targetInfo) {
        return null;
    }

    public RequestResult updateTargetBillEntryList(String fid, List<Map<String, Object>> entryList) {
        //修改收料数量，仓库
        List<Map<String, Object>> detailEntityList = new ArrayList<>();
        for (Map<String, Object> entry : entryList) {
            Map<String,Object> detailEntity = new LinkedHashMap<>();
            detailEntity.put("FEntryId", entry.get("ppBomEntryId"));//行id 必填
            detailEntity.put("FUseRate", 100);//使用比例
            detailEntityList.add(detailEntity);
        }
        //修改单据
        DataObject order = new DataObject(FormIdEnum.prdPpBom);
        order.setAttribute("FID", fid);
        order.setIsDeleteEntry(false);
        order.setAttribute("FENTITY", detailEntityList);//订单明细
        try {
            Map<String, Object> result = sqlTool.editForDataObject(order);
            if (result.get("code").toString().equals("200")){
                return RequestResult.ofSuccess(result);
            }else {
                return RequestResult.ofError(result.get("msg").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProcedureException("无法调用云星空接口");
        }
    }

    public void freshPpBomInfo(ArrayList<HashMap<String, Object>> changeBomList, List<Map<String, Object>> insertList, List<Map<String, Object>> deleteList, String ppBomNumber){
        //处理新增数据
        for (Map<String, Object> insert : insertList) {
            HashMap<String, Object> info = new HashMap<>();
            info.put("changeType", "1");//变更类型
            info.put("itemNumber", insert.get("itemNumber"));//物料编码
            info.put("numerator", insert.get("numerator"));//分子
            info.put("denominator", insert.get("denominator"));//分母
            info.put("ppBomNumber", ppBomNumber);//生产用料清单编号
            info.put("materialType", "1");//子项类型
            info.put("dosageType", "3");//用量类型
            info.put("issueType", "1");//发料方式
            info.put("overControlMode", "1");//超发控制方式
            changeBomList.add(info);
        }
        //处理删除数据
        for (HashMap<String, Object> changeBom : changeBomList) {
            String itemNumber = changeBom.get("itemNumber").toString();
            for (Map<String, Object> delete : deleteList) {
                String deleteItemNumber = delete.get("itemNumber").toString();
                if (deleteItemNumber.equals(itemNumber)){
                    changeBom.put("changeType", "5");
                }
            }
        }
    }

    /**
     *
     * @param billNo 单据编号
     */
    @Override
    public void postPosition(String billNo) {
        //免检的物料直接下推采购入库
    }

    /**
     *
     * @param billNo 单据编号
     */
    @Override
    public void auditTargetBill(String billNo) {
        //提交审核
        sqlTool.auditBill(billNo, FormIdEnum.ppBomChange);
    }

    /**
     * @param fid 单据id
     */
    @Override
    public void deleteTargetBill(String fid) {
        sqlTool.deleteBill(fid, FormIdEnum.stkInStock);
    }


}
